//
//  ZCMeFeedbackVC.m
//  zctx
//
//  Created by apple on 15/12/2.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeFeedbackVC.h"

@interface ZCMeFeedbackVC ()
{
    UIBarButtonItem *rightButtonItem;
}
@end

@implementation ZCMeFeedbackVC

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.view.shiftHeightAsDodgeViewForMLInputDodger = 50.0f;
    [self.view registerAsDodgeViewForMLInputDodger];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCMeFeedbackVC"];
    [MTA trackPageViewBegin:@"ZCMeFeedbackVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCMeFeedbackVC"];
    [MTA trackPageViewEnd:@"ZCMeFeedbackVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"用户反馈";
    self.view.backgroundColor =[UIColor colorWithHexString:@"#fafcfb"];

    self.BgView.layer.borderColor = RGB(239, 239, 239).CGColor;
    self.lineView.backgroundColor = RGB(239, 239, 239);
    self.BgView.layer.borderWidth = 1;
    [self initnavegation];
    self.textView.placeholder = @"留下您宝贵意见，您的意见一旦被采纳，惊喜大礼等你拿哦！";
    
    if (UserModel.mobile.length>1) {
        self.textField.text = UserModel.mobile;
    }else{
        self.textField.text = @"";
    }
}

#pragma mark 导航栏
-(void)initnavegation{

    rightButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonPressed)];
    rightButtonItem.enabled=NO;
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = 16;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightButtonItem];
    }else{
        self.navigationItem.rightBarButtonItem = rightButtonItem;
    }
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 提交
-(void)rightButtonPressed{
    NSLog(@"点击了提交");
    if (self.textField.text.length<=0) {
        [SVProgressHUD showErrorWithStatus:@"请填写联系方式"];
        return;
    }
    [NetworkManager requestMembersubfeedbackWithMid:UserModel.mid contact:self.textField.text content:self.textView.text success:^(NSDictionary *data) {
        NSString *err = [NSString stringWithFormat:@"%@",[data objectForKey:@"err"]];
        if ([err isEqualToString:@"0"]) {
            [SVProgressHUD showSuccessWithStatus:@"发送成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:@"发送出错了"];
        }
    } failure:^(NSError *error) {
    }];

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSMutableString * changedString=[[NSMutableString alloc]initWithString:textView.text];
    [changedString replaceCharactersInRange:range withString:text];
    
    if (changedString.length!=0) {
        rightButtonItem.enabled=YES;
//        self.navigationItem.rightBarButtonItem.enabled=YES;
    }else{
        rightButtonItem.enabled=NO;
//        self.navigationItem.rightBarButtonItem.enabled=NO;
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
